package com.ipzoe.web.controller.open;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import com.ipzoe.mem.domain.Member;
import com.ipzoe.mem.service.IMemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/sobot" )
public class ZhichiApiController {

    @Autowired
    private IMemberService memberService;

    @Value("${open.sobot.secretKey}")
    private String secretKey;

    @PostMapping("/checkIvr")
    public Map<String, String> checkIvr(String secretKey, String phoneNo, String content){

        log.info(StrUtil.format("req /api/sobot/checkIvr secretKey={},phoneNo={},content={}", secretKey, phoneNo, content));

        Map<String, String> result = Maps.newHashMap();

        if(!this.secretKey.equals(secretKey)){
            result.put("retCode", "999999");
            result.put("retMsg","非法的secretKey");
            return result;
        }

        if(StrUtil.isEmpty(content)){
            result.put("retCode", "999999");
            result.put("retMsg","content为空");
            return result;
        }

        //查询会员
        LambdaQueryWrapper<Member> lqw = new QueryWrapper<Member>().lambda()
                .eq(Member::getMemberNumLast, content);
        Member member = memberService.getOne(lqw);
        if(member == null){
            result.put("retCode", "999999");
            result.put("retMsg","会员不存在");
            return result;
        }

        result.put("retCode", "000000");
        result.put("retMsg",String.valueOf(member.getId()));
        return result;

    }


}
